/*
 * ConsultaAdicionarAlterar.java
 *
 * Created on Jan 20, 2012, 11:53:12 AM
 */
package presentation.view.consulta;

import domain.controller.ConsultaController;
import domain.controller.MedicoController;
import domain.controller.PacienteController;
import domain.pojo.Consulta;
import javax.swing.JOptionPane;
import presentation.view.JanelaInicial;
import presentation.view.MainFrame;


/**
 *
 * @author Owner
 */
public class ConsultaAdicionarAlterar extends javax.swing.JPanel {
    private Consulta consulta = null;
    private String especialidade = null;
    
    MedicoController mc = new MedicoController();
    PacienteController pc = new PacienteController();
    ConsultaController cc = new ConsultaController();
    
    /** Creates new form ConsultaAdicionarAlterar */
    public ConsultaAdicionarAlterar() {
	initComponents();
	jRadioButton2.setSelected(true);
    }
    
    public ConsultaAdicionarAlterar(String str) {
	this.especialidade = str;
	initComponents();
	jRadioButton2.setSelected(true);
    }
    
	
    public ConsultaAdicionarAlterar(Consulta consulta){
	this.consulta = consulta;
	initComponents();
	if( consulta == null){
	    jButton2.setText(" Adicionar ");
	} else {
	    jTextField1.setText(consulta.getCodigo());	    
	    //TODO inserir informacoes do combobox (selecionar)
	    jButton2.setText(" Atualizar ");
	}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox(mc.getNomes());
        jComboBox2 = new javax.swing.JComboBox(pc.getNomes());
        jLabel4 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adicionar nova", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel1.setText("Código");

        jTextField1.setColumns(20);

        jLabel2.setText("Médico");

        jLabel3.setText("Paciente");

        jButton1.setText("Voltar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Limpar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Salvar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel4.setText("Finalizado");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Sim");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Não");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jRadioButton2))
                            .addComponent(jComboBox2, 0, 174, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 174, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addGap(22, 22, 22))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(79, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(64, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(50, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	MainFrame.getInstance().alterarPainel(MainFrame.getInstance().getPainelAnterior(), null);
	MainFrame.getInstance().retiraPilhaPainel();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
	ConsultaController cc = new ConsultaController();
	Consulta consultaNova = new Consulta();
	consultaNova.setCodigo(jTextField1.getText().toString());
	consultaNova.getMedico().setId(mc.getIdFromNome(jComboBox1.getSelectedItem().toString()));
	consultaNova.getPaciente().setId(pc.getIdFromNome(jComboBox2.getSelectedItem().toString()));
	if(validaBranco()){
	    if(consulta == null){
		if(!cc.existeConsulta(consultaNova.getPaciente().getId(), consultaNova.getMedico().getId())){
		    cc.incluir(consultaNova);
		    JOptionPane.showMessageDialog(null, "Consulta atualizada com sucesso", "POO", JOptionPane.INFORMATION_MESSAGE);
		    MainFrame.getInstance().alterarPainel(new ConsultaListar(), JanelaInicial.getInstance());
		}else{
		    JOptionPane.showMessageDialog(null, "Essa consulta já existe.", "POO", JOptionPane.INFORMATION_MESSAGE);
		}
	    }else{
		if( JOptionPane.showConfirmDialog(null, "Confirma Atualização da Consulta?", "POO", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION ){
		    cc.atualizar(consultaNova);
		    JOptionPane.showMessageDialog(null, "Consulta atualizada com sucesso", "POO", JOptionPane.INFORMATION_MESSAGE);
		    MainFrame.getInstance().alterarPainel(new ConsultaListar(), JanelaInicial.getInstance());
		}
	    }
	}
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
	jTextField1.setText("");
	jComboBox1.setSelectedIndex(0);
	jComboBox2.setSelectedIndex(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private boolean validaBranco(){
	if(jTextField1.getText().equals("")){
	    JOptionPane.showMessageDialog(null, "Você deve inserir um código", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	}else if(jComboBox1.getSelectedItem() == null){
	    JOptionPane.showMessageDialog(null, "Você deve selecionar um médico", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	}else if(jComboBox2.getSelectedItem() == null){
	    JOptionPane.showMessageDialog(null, "Você deve selecionar um paciente", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	}else if(!jRadioButton2.isSelected()){
	    JOptionPane.showMessageDialog(null, "Você deve deixar a opção finalizado como \"não\"", "POO", JOptionPane.ERROR_MESSAGE);
	    return false;
	}
    	return true;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
